<?php
Registry::get('tpl')->assign("action", $action);
if (empty($_SESSION['campaign_item'])) {
	$_SESSION['campaign_item'] = array();
}else
{
	foreach( $_SESSION['campaign_item'] as $k => $v )
	{
		if( !is_array($v) )
			$$k = trim($v);
	}
}	
switch( $step )
{
	
	case 'step1':
		if( $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
			if( $camCateId && $camTitle )
			{
				$_SESSION['campaign_item']['camCateId'] = $camCateId;
				$_SESSION['campaign_item']['camTitle'] = $camTitle;
				$_SESSION['campaign_item']['camLocId'] = $camLocId;
				$_SESSION['campaign_item']['camIntro'] = $camIntro;
				$_SESSION['campaign_item']['camRules'] = $camRules;
				$_SESSION['campaign_item']['camNotes'] = $camNotes;								
				header("Location: /dashboard/campaign.html?step=step2");
			}else
				fn_set_notification('E', '非法存取', 'index.html');
			exit;	
		}
		if( $camClientId && $camType )
		{			

			fn_GetClientData($camClientId);
			
			$sql = "SELECT * FROM aams_campaign_cate ORDER BY camCateId ASC";
			$aryCate = $_db->GetAll($sql);
			if( $aryCate )
			{
				Registry::get('tpl')->assign("aryCate", $aryCate);
			}
			
			$sql = "SELECT * FROM aams_campaign_loc ORDER BY camLocId ASC";
			$aryLoc = $_db->GetAll($sql);
			if( $aryLoc )
			{
				Registry::get('tpl')->assign("aryLoc", $aryLoc);
			}
			
			Registry::get('tpl')->assign("main", "dashboard/campaign_step01.html");
			Registry::get('tpl')->display("template.html"); 	
		}else
			fn_set_notification('E', '操作時間已經逾時，請重新登入操作', 'index.html');
	break;
	
	case 'step2':
		if( $camCateId && $camTitle )
		{
			fn_GetClientData($camClientId);
			
			Registry::get('tpl')->assign("main", "dashboard/campaign_step02.html");
			Registry::get('tpl')->display("template.html"); 				
		}else
			fn_set_notification('E', '操作時間已經逾時，請重新登入操作', 'index.html');
	break;
	
	default:
		if( $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
		
			if( $camClientId && $camType )
			{
				$_SESSION['campaign_item']['camClientId'] = $camClientId;
				$_SESSION['campaign_item']['camType'] = $camType;
				header("Location: /dashboard/campaign.html?step=step1");
			}else
				fn_set_notification('E', '非法存取', 'index.html');
			exit;	
		}
		$fb_pages = fn_loadFacebookPages($uid);
		if( sizeof($fb_pages) > 0 ){
			foreach( $fb_pages as $k => $v )
			{
				$fb_pages_id_ary[] = $v['id']; 	
			} 
			$sql_extra = ' OR c.cpages_id IN('.implode(",", $fb_pages_id_ary).')';
		}
		$sql = "SELECT c.cid, c.cunit FROM aams_client as c 
						LEFT JOIN aams_user as u ON (u.uid=c.cuid) 
						WHERE u.uid = '$uid' $sql_extra
						ORDER BY c.cid ASC";
		$aryClient = $_db->GetAll($sql);	
		Registry::get('tpl')->assign("aryClient", $aryClient);
		Registry::get('tpl')->assign("main", "dashboard/campaign_step00.html");
		Registry::get('tpl')->display("template.html"); 	
	break;	
}


function fn_GetClientData( $ClientId )
{
	global $_db;	
	$sql = "SELECT cid, cunit FROM aams_client WHERE cid='$ClientId'";
	$aryClient = $_db->GetRow($sql);
	if( $aryClient )
	{
		Registry::get('tpl')->assign("aryClient", $aryClient);
	}else
		fn_set_notification('E', '操作時間已經逾時，請重新登入操作', '/dashboard/index.html');	
	
	return;
}
?>